package com.kgc.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.annotation.CheckPermission;
import com.kgc.dao.ProductMapper;
import com.kgc.entity.BuyCart;
import com.kgc.entity.BuyCartProduct;
import com.kgc.util.ResultAjax;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author :qinya
 * @PackageName:com.kgc.controller
 * @ClassName: BuyCartController
 * @Description: TODO
 * @date : 2025/5/26,16:22
 */
@RestController
@RequestMapping("/buyCart")
public class BuyCartController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/find")
    @CheckPermission({"1", "0"})
    public ResultAjax getBuyCart(@RequestParam("userId") String userId) throws JsonProcessingException {
        String buyCartId = userId + "buyCart";
        ObjectMapper om = new ObjectMapper();
        if (!stringRedisTemplate.hasKey(buyCartId)) {
            stringRedisTemplate.opsForValue().set(buyCartId, om.writeValueAsString(new BuyCart(userId)));
        }
        return ResultAjax.success().add("buyCart", om.readValue(stringRedisTemplate.opsForValue().get(buyCartId), BuyCart.class));
    }

    @GetMapping("/add")
    @CheckPermission({"1", "0"})
    public ResultAjax addBuyCartProduct(@RequestParam Map<String, String> params) throws JsonProcessingException {
        String buyCartId = params.get("userId") + "buyCart";
        String productId = params.get("productId");
        int count = 1;
        if (params.containsKey("count")) {
            count = Integer.parseInt(params.get("count"));
        }
        ObjectMapper om = new ObjectMapper();
        BuyCart buyCart = om.readValue(stringRedisTemplate.opsForValue().get(buyCartId), BuyCart.class);
        //购物车中有这个商品
        if (buyCart.getMap().containsKey(productId)) {
            //加数量
            buyCart.getMap().get(productId).setCount(buyCart.getMap().get(productId).getCount() + count);
        }//购物车中没有这个商品
        else {
            //加一个商品，默认数量为1
            BuyCartProduct buyCartProduct = productMapper.selectBuyCartProductById(productId);
            buyCartProduct.setCount(count);
            buyCart.getMap().put(productId, buyCartProduct);
        }
        //更新redis
        try {
            stringRedisTemplate.opsForValue().set(buyCartId, om.writeValueAsString(buyCart));
            return ResultAjax.success().add("addStatus", true);
        } catch (Exception e) {
            return ResultAjax.error().add("addStatus", false);
        }
    }

    @PostMapping("/edit")
    @CheckPermission({"1", "0"})
    public ResultAjax editBuyCartProduct(@RequestBody Map<String, String> params) throws JsonProcessingException {
        String buyCartId = params.get("userId") + "buyCart";
        String productId = params.get("productId");
        int count = Integer.parseInt(params.get("count"));
        ObjectMapper om = new ObjectMapper();
        BuyCart buyCart = om.readValue(stringRedisTemplate.opsForValue().get(buyCartId), BuyCart.class);
        BuyCartProduct buyCartProduct = buyCart.getMap().get(productId);
        buyCartProduct.setCount(count);
        //更新redis
        try {
            stringRedisTemplate.opsForValue().set(buyCartId, om.writeValueAsString(buyCart));
            return ResultAjax.success().add("editStatus", true);
        } catch (Exception e) {
            return ResultAjax.error().add("editStatus", false);
        }
    }

    @PostMapping("/del")
    @CheckPermission({"1", "0"})
    public ResultAjax delBuyCartProduct(@RequestBody Map<String, String> params) throws JsonProcessingException {
        String buyCartId = params.get("userId") + "buyCart";
        String productId = params.get("productId");
        ObjectMapper om = new ObjectMapper();
        BuyCart buyCart = om.readValue(stringRedisTemplate.opsForValue().get(buyCartId), BuyCart.class);
        buyCart.getMap().remove(productId);
        //更新redis
        try {
            stringRedisTemplate.opsForValue().set(buyCartId, om.writeValueAsString(buyCart));
            return ResultAjax.success().add("delStatus", true);
        } catch (Exception e) {
            return ResultAjax.error().add("delStatus", false);
        }
    }

    @PostMapping("/empty")
    @CheckPermission({"1", "0"})
    public ResultAjax emptyBuyCart(@RequestBody String userId) throws JsonProcessingException {
        //清空购物车
        ObjectMapper om = new ObjectMapper();
        try {
            stringRedisTemplate.opsForValue().set(userId + "buyCart",om.writeValueAsString(new BuyCart()));
            return ResultAjax.success().add("isEmpty",true);
        }catch (Exception e){
            return ResultAjax.success().add("isEmpty",false);
        }

    }
}
